package com.origin.chaos.common.bean.vo;

import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;

/**
 * @author: tao.ding
 * @date 2021-07-16
 */
@Data
@Builder
public class Result<T> {
    private static final String SUCCESS_MESSAGE = "success";

    private static final class Code {
        private static final Integer SUCCESS = 1000;

        /**
         * 未进行任何操作，比如发送验证码，判断已经发送过了，不再发送
         */
        private static final Integer NO_EFFECT = 1001;

        private static final Integer ERROR = 4000;

        private static final Integer NOT_LOGIN = 4001;
    }

    @ApiModelProperty("状态码(1000:成功;4000:失败)")
    private int code;

    @ApiModelProperty("消息")
    private String message;

    @ApiModelProperty("服务器时间戳")
    private long timestamp;

    @ApiModelProperty("数据结构体")
    private T body;

    public static <T> Result<T> ofBody(T body) {
        return Result.<T>builder()
                .code(Code.SUCCESS)
                .message(SUCCESS_MESSAGE)
                .timestamp(System.currentTimeMillis())
                .body(body)
                .build();
    }

    public static Result defaultSuccess() {
        return Result.builder()
                .code(Code.SUCCESS)
                .message(SUCCESS_MESSAGE)
                .timestamp(System.currentTimeMillis())
                .build();
    }

    public static Result ofErrorMessage(String message) {
        return Result.builder()
                .code(Code.ERROR)
                .message(message)
                .timestamp(System.currentTimeMillis())
                .build();
    }

    public static <T> Result<T> ofError(T body, String message) {
        return Result.<T>builder()
                .body(body)
                .code(Code.ERROR)
                .message(message)
                .timestamp(System.currentTimeMillis())
                .build();
    }

    public static Result notLogin() {
        return Result.builder()
                .code(Code.NOT_LOGIN)
                .message("请登录")
                .timestamp(System.currentTimeMillis())
                .build();
    }

    public static <T> Result<T> noEffect(T body) {
        return Result.<T>builder()
                .body(body)
                .code(Code.NO_EFFECT)
                .message(SUCCESS_MESSAGE)
                .timestamp(System.currentTimeMillis())
                .build();
    }
}
